package com.bana.fvs.service;

import com.bana.fvs.code.SystemLogType;
import com.bana.fvs.protocol.SetCamera;
import com.bana.fvs.protocol.SetHorn;
import com.bana.fvs.protocol.SetScreen;
import com.bana.fvs.task.SocketTask;
import com.bana.sys.code.SendResult;
import com.bana.sys.service.LogService;
import com.bana.sys.socket.SocketResult;
import com.bana.sys.utils.Convert;
import onbon.bx05.Bx5GEnv;
import onbon.bx05.Bx5GScreenClient;
import onbon.bx05.Bx5GScreenProfile;
import onbon.bx05.area.TextCaptionBxArea;
import onbon.bx05.area.page.ImageFileBxPage;
import onbon.bx05.file.ProgramBxFile;
import onbon.bx05.utils.DisplayStyleFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.UUID;

@Service
public class LinkDeviceService {

    @Resource
    private LogService logService;

    private static Bx5GScreenClient screen;

    private SendResult send(int protocolId,long timeStamp,byte[] buffer,String joinId)
    {
        SocketResult sr=SocketTask.DeviceHandler.sendTcp(protocolId,timeStamp,buffer,10000);
        logService.insertSocketLog( protocolId,
                0,0,Convert.toHex(buffer),sr.getResult().ordinal(), Convert.toHex(sr.getReceive()),joinId);
        return sr.getResult();
    }
    public SendResult setLedSpeed(String token,int card,int speed,int size,int sbcj,String ipdz,int dk) throws IOException {
        String joinId= UUID.randomUUID().toString();
        //湖南led
        if(sbcj==1)
            try {
                if (screen == null) {
                    Bx5GEnv.initial("log.properties", 30000);
                    screen = new Bx5GScreenClient("MyScreen");
                }
                if (screen.connect(ipdz, dk)) {
                    if(speed==0)
                    {
                        screen.turnOff();
                    }
                    else
                    {
                        screen.turnOn();
                        String filePath = String.format("D:\\wq\\pic\\%d_80.bmp", speed);

                        Bx5GScreenProfile profile = screen.getProfile();

                        DisplayStyleFactory.DisplayStyle[] styles = DisplayStyleFactory.getStyles().toArray(new DisplayStyleFactory.DisplayStyle[0]);
                        // 创建节目文件
                        ProgramBxFile p000 = new ProgramBxFile("P000", profile);
                        // 是否显示节目边框
                        p000.setFrameShow(false);

                        int posX = 48;
                        int posY = 10;
                        TextCaptionBxArea tArea = new TextCaptionBxArea(posX, posY, 80, 80, screen.getProfile());
                        tArea.setFrameShow(false);
                        ImageFileBxPage iPage = new ImageFileBxPage(filePath);
                        iPage.setDisplayStyle(styles[1]);
                        iPage.setStayTime(1000000000);
                        tArea.addPage(iPage);
                        p000.addArea(tArea);

                        ArrayList<ProgramBxFile> plist = new ArrayList<>();
                        plist.add(p000);
                        screen.deletePrograms();
                        screen.writePrograms(plist);
                    }
                    logService.insertSystemLog(token, SystemLogType.SetScreen, String.format("led 发送成功 %s %d %d",ipdz,dk,speed), joinId);
                    screen.disconnect();
                    return SendResult.Success;
                } else {
                    logService.insertSystemLog(token, SystemLogType.SetScreen, String.format("led 连接失败 %s %d %d",ipdz,dk,speed), joinId);
                    return SendResult.Disconnection;
                }
            } catch (Exception ex) {
                logService.insertSystemLog(token, SystemLogType.SetScreen, ex.getMessage(), joinId);
                return SendResult.SendFailed;
            }
        else
        {
            SetScreen setLedSpeed=new SetScreen();
            byte[] buffer=setLedSpeed.request(card,speed,size);
            logService.insertSystemLog(token,SystemLogType.SetScreen,String.format("限速屏 %d %d",card,speed),joinId);
            return send(setLedSpeed.getId(),setLedSpeed.getStampTime(),buffer,joinId);
        }
    }

    public SendResult setCameraSpeed(String token,String cameraIp,int smallLow,int smallHigh,int bigLow,int bigHigh) throws IOException {
        SetCamera setCamera =new SetCamera();
        byte[] buffer=setCamera.request(cameraIp,smallLow,smallHigh,bigLow,bigHigh);
        String joinId= UUID.randomUUID().toString();
        logService.insertSystemLog(token, SystemLogType.SetCamera,String.format("抓拍相机 %s %d %d %d %d",cameraIp,smallLow,smallHigh,bigLow,bigHigh),joinId);
        return send(setCamera.getId(),setCamera.getStampTime(),buffer,joinId);
    }

    public SendResult setHornMusic(String token,int id,int file,int volume) throws IOException {
        SetHorn setHorn =new SetHorn();
        volume+=5;
        byte[] buffer=setHorn.request(id, file, volume);
        String joinId= UUID.randomUUID().toString();
        logService.insertSystemLog(token,SystemLogType.SetHorn,String.format("号角 %d %d %d",id,file,volume),joinId);
        return send(setHorn.getId(),setHorn.getStampTime(),buffer,joinId);
    }
}
